<?php
namespace app\api\controller;
use app\api\controller\BaseController;
use app\common\errcode\ApiErrCode;
use app\common\errcode\ApiErrMsg;
use think\Loader;

class VideoController extends BaseController
{
    protected $videoModel;

    public function __construct()
    {
        parent::__construct();
        $this->videoModel = new \app\api\model\VideoModel();
    }

    /**
     * 视频列表
     * @param int    $page          页码
     * @param int    $pagesize      分页数量
     * @param string $status        前测状态
     * @param string $tag           前测标签
     * @param string $userid        上传人
     * @param int    $brand_id      品牌id
     * @param int    $product_id    产品id
     * @param string $name          视频名
     */
    public function index()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->videoModel->getList($this->userid,$this->departmentId,$data));         
            }        
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }   
    }

    /**
     * 视频上传
     * @param int $brand_id 品牌id
     * @param int $product_id 产品id
     * @return json
     */
    public function upload()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Video');
                $check = $validate->scene('upload')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->videoModel->upload($this->userid,$this->departmentId,$data)){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);
                }
                return $this->jsonJump(ApiErrCode::UPLOAD_FAIL,ApiErrMsg::UPLOAD_FAIL);    
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 视频推送
     * @param array $advertiser_ids
     * @param array $video_ids
     */
    public function push()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Video');
                $check = $validate->scene('push')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->videoModel->push($this->userid,$this->departmentId,$data)){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);
                }
                return $this->jsonJump(ApiErrCode::PUSH_FAIL,ApiErrMsg::PUSH_FAIL);
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        } 
    }

    /**
     * 推送记录
     * @param int $id 视频id
     * @param int $page 页码
     * @param int $pagesize 页码大小
     * @return \think\response\Json
     */
    public function log()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Video');
                $check = $validate->scene('log')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->videoModel->getLog($this->userid,$this->departmentId,$data));
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }   
    }

    /**
     * @param string $advertiser_id
     * @return \think\response\Json
     */
    public function advertiser()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Video');
                $check = $validate->scene('advertiser')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                $res = (new \app\api\model\AdvertiserModel)->get_advertiser_info_bak($data);
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$res);      
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 重新检测
     * @param int   $id
     */
    public function check_again()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Video');
                $check = $validate->scene('check_again')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->videoModel->check_again($data['id'])){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);        
                }
                return $this->jsonJump(ApiErrCode::CHECK_FAIL,ApiErrMsg::CHECK_FAIL);      
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 推送历史
     */
    public function history()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS,$this->videoModel->getHistory($this->userid,$this->departmentId,$data));         
            }    
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }
    }

    /**
     * 删除视频
     * @param int $id
     */
    public function delete()
    {
        try {
            if($this->request->isPost()){
                $data = $this->request->post();
                $validate = Loader::validate('Video');
                $check = $validate->scene('delete')->check($data);
                if(!$check) return $this->jsonJump(ApiErrCode::PARAM_ERROR,ApiErrMsg::PARAM_ERROR.','.$validate->getError());
                if($this->videoModel->delete_video($this->userid, $data)){
                    return $this->jsonJump(ApiErrCode::SUCCESS,ApiErrMsg::SUCCESS);        
                }
                return $this->jsonJump(ApiErrCode::DELETE_FAIL,ApiErrMsg::DELETE_FAIL);      
            }
        } catch (\Exception $e) {
            \app\common\model\CommonModel::savelog($this->userid,$e,$this->request->path());
            return $this->jsonJump(ApiErrCode::SYSTEM_ERROR,ApiErrMsg::SYSTEM_ERROR . ',错误信息:'. $e->getMessage());
        }    
    }
}